home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Printer Drivers… / HPXL / PackageRoutines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  9.1 KB  |  336 lines  |  [TEXT/MPS ]

  1. /* ==========================================================================================
  2.  
  3.     C FILE:            PackageRoutines.c
  4.  
  5.     COPYRIGHT:        (c) 1992-94 Apple Computer Inc.
  6.                         All rights reserved.
  7.  
  8.     PURPOSE:        This file contains the routines to package data to be sent to
  9.                     the plotter. These routines send the necessary HPGL commands
  10.                     to the plotter to achieve the desired output.
  11.                         
  12.     HISTORY:        Date            By            Description
  13.                     -----            ---        --------------------------------------
  14.                     1/20/92            Harita        Initial release
  15.                     12/20/93            dmh            Sync'd up for GX 1.0b3.
  16.                      8/28/94            dmh            Sync'd up for GX 1.0.1.
  17.                     6/14/96            cn            Updated to support Universal Interfaces 2.1.
  18.  
  19. ========================================================================================== */
  20.  
  21. #include <stdio.h>
  22. #include <Types.h>
  23. #include <OSUtils.h>
  24. #include <Memory.h>
  25. #include <Errors.h>
  26. #include <ToolUtils.h>
  27. #include <FixMath.h>
  28.  
  29. #include <GXTypes.h>
  30. #include <GXErrors.h>
  31. #include <GXGraphics.h>
  32. #include <GXMath.h>
  33.  
  34. #include <GXPrinting.h>
  35. #include <GXPrinterDrivers.h>
  36.  
  37.  
  38. #include "ResourceDefines.h"
  39. #include "GlobalsDefs.h"
  40. #include "PackageRoutines.h"
  41.  
  42. extern SpecDrvrGlobals        gGlobals;
  43.  
  44.  
  45.  
  46. /* =================================== INTERFACE ROUTINES =================================== */
  47.  
  48. OSErr    MyDumpBuffer(
  49.     short                n)
  50. {
  51.     OSErr                    anErr = noErr;
  52.  
  53.     if ((n + gGlobals.bufferSize) >= kMaxBufferSize)
  54.         {
  55.         anErr = Send_GXBufferData(&gGlobals.buffer[0], gGlobals.bufferSize, gxNoBufferOptions);        
  56.         if (anErr != noErr)
  57.             return(anErr);
  58.         gGlobals.bufferSize = 0;
  59.         }
  60.     BlockMove(gGlobals.tempBuffer, &gGlobals.buffer[gGlobals.bufferSize], n);
  61.     gGlobals.bufferSize += n;
  62.     
  63.     
  64. }
  65. /* ======= StartOfPagePlot =======
  66.  
  67.     StartOfPagePlot is called to send the HPGL commands to the device for beginning a new page.
  68.     This routine issues a re-initialization command to the plotter.
  69. */
  70. OSErr StartOfPagePlot (                //    (out)    error code                
  71. )
  72. {
  73.     OSErr                    anErr = noErr;
  74.     short                    n;
  75.     Str255                    theStr;
  76.  
  77.     GetIndString(theStr, kHPXLCmndStringsID, kInitIdx);
  78.     theStr[theStr[0]+1] = 0;
  79.     n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
  80.     anErr = MyDumpBuffer(n);
  81.     if (anErr != noErr)
  82.         return(anErr);
  83.  
  84.     GetIndString(theStr, kHPXLCmndStringsID, kSetPenIdx);
  85.     theStr[theStr[0]+1] = 0;
  86.     n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], 1);
  87.     anErr = MyDumpBuffer( n);
  88.  
  89.  
  90.     return(anErr);
  91.     
  92.     
  93. } /* end of StartOfPagePlot */
  94.  
  95.  
  96. /* ======= EndOfPagePlot =======
  97.  
  98.     EndOfPagePlot is called to send the HPGL commands to the device for ending the page just
  99.     plotted.  This routine issues a "Return pen to carousel…" HPGL command.
  100. */
  101.  
  102. OSErr EndOfPagePlot (                //    (out)    error code                
  103. )
  104. {
  105.     OSErr        anErr = noErr;
  106.     short        n;
  107.     Str255        theStr;
  108.     
  109.   GetIndString(theStr, kHPXLCmndStringsID, kSetPenIdx);
  110.   theStr[theStr[0]+1] = 0;
  111.   n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], 0);
  112.   anErr = MyDumpBuffer(n);
  113.  
  114.   GetIndString(theStr, kHPXLCmndStringsID, kTermPageIdx);
  115.   theStr[theStr[0]+1] = 0;
  116.   n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
  117.   anErr = MyDumpBuffer(n);
  118.  
  119.   anErr = Send_GXBufferData(&gGlobals.buffer[0], gGlobals.bufferSize, gxNoBufferOptions);        
  120.   if (anErr != noErr)
  121.       return(anErr);
  122.   gGlobals.bufferSize = 0;
  123.     
  124.   return(anErr);
  125.  
  126.  
  127. }    /* EndOfPagePlot */
  128.  
  129.  
  130. /* ======= PlotLine =======
  131.  
  132.     PlotLine is called to send the HPGL2 commands to the device for plotting a line.
  133.     The line to draw is specified by the lineToDraw shape.
  134. */
  135. OSErr PlotLine (                        //    (out)    error code                    
  136.     gxShape                lineToDraw,        //    (in)    Graphics shape describing the line to draw
  137.     gxMapping            theMapping)        //    (in)    mapping use to scale points to device resolution
  138. {
  139.     OSErr        anErr = noErr;
  140.     gxLine        theLine;
  141.     long        startX;
  142.     long        startY;
  143.     long        endX;
  144.     long        endY;
  145.     short        n;
  146.     Str255        theStr;
  147.     
  148.     GetIndString(theStr, kHPXLCmndStringsID, kLineIdx);
  149.       theStr[theStr[0]+1] = 0;
  150.     
  151.     
  152.     /* Get the points that compose the line */
  153.     GXGetLine(lineToDraw, &theLine);
  154.     
  155.     /* Scale the points to the resolution of the device */
  156.     MapPoints(&theMapping, 2, (gxPoint *) &theLine);
  157.     
  158.     startX = FixedToInt(theLine.first.x);
  159.     startY = FixedToInt(theLine.first.y);
  160.     endX = FixedToInt(theLine.last.x);
  161.     endY = FixedToInt(theLine.last.y);
  162.     
  163.     /* Now package the data */
  164.     n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], startX, startY, endX, endY);
  165.     anErr = MyDumpBuffer(n);
  166.  
  167.     
  168.     return(anErr);
  169. }    /* PlotLine */
  170.  
  171.  
  172. /* ======= PlotRectangle =======
  173.  
  174.     PlotRectangle is called to send the HPGL commands to the device for plotting a non-filled
  175.     rectangle.  The rectangle to draw is specified by the rectToDraw shape.
  176. */
  177. OSErr PlotRectangle (                    //    (out)    error code                    
  178.     gxShape                rectToDraw,        //    (in)    Graphics shape describing the rectangle to draw
  179.     gxMapping            theMapping)        //    (in)    mapping use to scale points to device resolution
  180. {
  181.     OSErr            anErr = noErr;
  182.     gxRectangle        theRectangle;
  183.     long            left;
  184.     long            top;
  185.     long            right;
  186.     long            bottom;
  187.     gxShapeFill        sFill;
  188.     short            n;
  189.     Str255            theStr;
  190.        
  191.         
  192.     /* Get the points that compose the line */
  193.     GXGetRectangle(rectToDraw, &theRectangle);
  194.     
  195.     /* Scale the points to the resolution of the device */
  196.     MapPoints(&theMapping, 2, (gxPoint *) &theRectangle);
  197.     
  198.     left = FixedToInt(theRectangle.left);
  199.     top = FixedToInt(theRectangle.top);
  200.     right = FixedToInt(theRectangle.right);
  201.     bottom = FixedToInt(theRectangle.bottom);
  202.     
  203.      sFill = GXGetShapeFill(rectToDraw);
  204.     if ((sFill == gxEvenOddFill ) || (sFill == gxWindingFill))
  205.         GetIndString(theStr, kHPXLCmndStringsID, kFilledRectIdx);
  206.     else
  207.         GetIndString(theStr, kHPXLCmndStringsID, kRectIdx);
  208.  
  209.     theStr[theStr[0]+1] = 0;
  210.     /* Now package the data */
  211.     n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], left, top, right,    bottom);
  212.     anErr = MyDumpBuffer(n);
  213.         
  214.     
  215.     return(anErr);
  216. }    /* PlotRectangle */
  217.  
  218.  
  219. /* ======= PlotPolygon =======
  220.  
  221.     PlotPolygon is called to send the HPGL commands to the device for plotting a non-filled
  222.     polygon.  The polygon to draw is specified by the polyToDraw shape.
  223. */
  224. OSErr PlotPolygon (                        //    (out)    error code                    
  225.     gxShape                polyToDraw,        //    (in)    Graphics shape describing the polygon to draw
  226.     gxMapping            theMapping)        //    (in)    mapping use to scale points to device resolution
  227. {
  228.     OSErr            anErr = noErr;
  229.     gxPolygons        *thePolys;
  230.     long            shapeSize;
  231.     gxShapeFill        sFill;
  232.     long            *p, contours, points, i;
  233.     Boolean            firstFlag;
  234.     short            startX, startY;
  235.     short            n;
  236.     Str255            theStr;
  237.     
  238.     sFill = GXGetShapeFill(polyToDraw);
  239.     
  240.  
  241.     /* Allocate enough space to hold the contents of the polygon shape */
  242.     
  243.     GXGetShapeStructure(polyToDraw, &shapeSize);
  244.     thePolys = (gxPolygons *) NewPtr(shapeSize);
  245.     if (thePolys == nil)
  246.        {
  247.          return(MemError());
  248.        }
  249.     
  250.     /* Place the polygon geometry into the newly allocated pointer */
  251.    GXGetPolygons(polyToDraw, thePolys);
  252.    p = (long*) thePolys;
  253.    contours = *p++;
  254.    firstFlag = true;
  255.  
  256.    while (contours--)
  257.       {
  258.       GXJobIdle(); // give the forground app some time
  259.       points = *p++;
  260.       /* Scale the points to the resolution of the device */
  261.       MapPoints(&theMapping, points, (gxPoint *) p);
  262.  
  263.       i = 0;
  264.       startX = FixedToInt(*p++);
  265.       startY = FixedToInt(*p++);
  266.       if ((sFill != gxOpenFrameFill) && (firstFlag))
  267.          GetIndString(theStr, kHPXLCmndStringsID, kInitPolyIdx);
  268.       else
  269.          GetIndString(theStr, kHPXLCmndStringsID, kInitLinesIdx);
  270.  
  271.        theStr[theStr[0]+1] = 0;
  272.      /* Now package the data */
  273.      n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], startX, startY);
  274.      anErr = MyDumpBuffer(n);
  275.      if (anErr != noErr) goto cleanup;
  276.       
  277.       for (i=1; i<points; ++i)
  278.           {
  279.           startX = FixedToInt(*p++);
  280.           startY = FixedToInt(*p++);
  281.           GetIndString(theStr, kHPXLCmndStringsID, kDataIdx);
  282.             theStr[theStr[0]+1] = 0;
  283.            n = sprintf(gGlobals.tempBuffer, (char *)&theStr[1], startX, startY);
  284.            anErr = MyDumpBuffer(n);
  285.             if (anErr != noErr) goto cleanup;
  286.           
  287.           if ((i+1) != points)
  288.              {
  289.                GetIndString(theStr, kHPXLCmndStringsID, kCommaIdx);
  290.                theStr[theStr[0]+1] = 0;
  291.              n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
  292.                 anErr = MyDumpBuffer(n);
  293.                  if (anErr != noErr) goto cleanup;
  294.              }
  295.           GXJobIdle(); // give foreground app some time
  296.           }
  297.        
  298.        if (sFill == gxOpenFrameFill)
  299.            GetIndString(theStr, kHPXLCmndStringsID, kTermLinesIdx);
  300.        else if (sFill == gxClosedFrameFill)
  301.            GetIndString(theStr, kHPXLCmndStringsID, kTermGlyphIdx);
  302.        else
  303.            {
  304.            GetIndString(theStr, kHPXLCmndStringsID, kTermPolyIdx);
  305.            firstFlag = false;
  306.            }
  307.           theStr[theStr[0]+1] = 0;
  308.          n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
  309.          anErr = MyDumpBuffer(n);
  310.           if (anErr != noErr) goto cleanup;
  311.       } // end of while loop
  312.       
  313.       if ((sFill != gxOpenFrameFill) && (sFill != gxClosedFrameFill))
  314.            {
  315.          GetIndString(theStr, kHPXLCmndStringsID, kTermPolysIdx);
  316.            theStr[theStr[0]+1] = 0;
  317.           n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
  318.           anErr = MyDumpBuffer(n);
  319.            if (anErr != noErr) goto cleanup;
  320.          
  321.          GetIndString(theStr, kHPXLCmndStringsID, kFillIdx);
  322.            theStr[theStr[0]+1] = 0;
  323.           n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
  324.           anErr = MyDumpBuffer(n);
  325.            if (anErr != noErr) goto cleanup;
  326.          }
  327.     
  328.     
  329. cleanup:
  330.     /* Deallocate the temporary pointer */
  331.     DisposePtr((Ptr) thePolys);
  332.     
  333.     
  334.     return(anErr);
  335. }    /* PlotPolygon */
  336.